package com.example.networkchangedemo.weight;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import com.example.networkchangedemo.R;

public class TipTopPopUpWindow extends PopupWindow {
    private int popupWidth;
    private int popupHeight;
    int[] location;
    private View view;
    FrameLayout flTip;

    public TipTopPopUpWindow(Activity context, int width, int height, boolean focusable){
        view = LayoutInflater.from(context).inflate(R.layout.layout_top_view, null);
        flTip = view.findViewById(R.id.fl_top);
        setContentView(view);
        setTouchable(true);
        setHeight(height);
        setWidth(width);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupWidth = view.getMeasuredWidth();
        popupHeight = view.getMeasuredHeight();
        location = new int[2];
        setOutsideTouchable(true);
        initView();
    }

    private void initView() {
        flTip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TipTopPopUpWindow.this.dismiss();
            }
        });
    }

    /**
     * 设置显示在v上方(以v的左边距为开始位置)
     * @param v
     */
    public void showUp(View v) {
        //获取需要在其上方显示的控件的位置信息
        v.getLocationOnScreen(location);
        //在控件上方显示
        TipTopPopUpWindow.this.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
    }
}
